package com.xiaoc.bulldozer.common.service.impl;

import com.xiaoc.bulldozer.common.dao.RoleMapper;
import com.xiaoc.bulldozer.common.model.Role;
import com.xiaoc.bulldozer.common.service.RoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Role Service 实现类
 * 
 * @author CodeGenerator
 * 
 * @date 2014-05-20 17:26:51
 * 
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.getRoleById(id);
    }
    
    public List<Role> getAllRoleList() {
        return roleMapper.getAllRoleList();
    }

    @Override
    public Map<Integer, Role> getAllRoleMap() {

        List<Role> roleList = roleMapper.getAllRoleList();
        if (CollectionUtils.isEmpty(roleList)) {
            return MapUtils.EMPTY_MAP;
        }
        
        Map<Integer, Role> roleMap = new HashMap<Integer, Role>();
        for (Role role : roleList) {
            roleMap.put(role.getId(), role);
        }
        
        return roleMap;
    }

    @Override
    public void save(Role role) {
        role.setUpdateTime(System.currentTimeMillis());
        roleMapper.save(role);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(System.currentTimeMillis());
        roleMapper.update(role);
    }
    
}
